﻿using ETA.Dtos;
using ETA.Entity;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ETA.Web.Controllers
{
    public class QAController : BaseController
    {
        private static IQuestionService _IQuestionService { set; get; }

        public QAController(IQuestionService IQuestionService)
        {
            _IQuestionService = IQuestionService;
        }

        //
        // GET: /QA/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult AddQuestion(string content, string contactWay)
        {
            var userId = string.Empty;
            var userName = "游客";
            SimpleUserDto loginUser = null;
            if ((loginUser = CurrentUser) != null)
            {
                userId = loginUser.UserId;
                userName = loginUser.UserName;
            }
            var res = _IQuestionService.CreateQuestion(
               new CreateQuestionRequest
               {
                   Question = new Question
                   {
                       UserId = userId,
                       UserName = userName,
                       QuestionContent = content.Trim(),
                       ContactWay = contactWay.Trim(),
                       CreateDate = DateTime.Now
                   }
               }
            );
            if (res.Result.IsSuccess)
            {
                TempData["Info"] = "true";
            }
            else
            {
                TempData["Info"] = res.Result.ErrMessage;
            }
            return RedirectToAction("Index");
        }


        public ActionResult GetQAList(int page, int pageSize = 10, string keyWord = null)
        {
            var list = new List<Question>();
            var count = 0;
            var response = _IQuestionService.QueryQuestionList(
            new QueryQuestionRequest
            {
                Page = page,
                PageSize = pageSize,
                KeyWord = keyWord,
                IsWeb=true
            }, out count
            );
            if (response.Result.IsSuccess)
            {
                list = response.QuestionList.ToList();
            }
            return JsonWithDate(list);
        }



        public ActionResult GetQACount(string keyWord = null)
        {
            var count = 0;
            var response = _IQuestionService.QueryQuestionList(
            new QueryQuestionRequest
            {
                KeyWord = keyWord,
                IsWeb = true
            }, out count
            );
            return JsonWithDate(count);
        }
    }
}
